package effective.java.item59;

import org.openjdk.jmh.annotations.*;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.SplittableRandom;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public class RandomPerformanceTest {

    private static final int ITERATIONS = 10_000_000;

    @State(Scope.Benchmark)
    public static class BenchmarkState {
        Random random = new Random();
        SplittableRandom splittableRandom = new SplittableRandom();
    }

    @Benchmark
    public void testRandom(BenchmarkState state) {
        for (int i = 0; i < ITERATIONS; i++) {
            state.random.nextInt();
        }
    }

    @Benchmark
    public void testThreadLocalRandom() {
        for (int i = 0; i < ITERATIONS; i++) {
            ThreadLocalRandom.current().nextInt();
        }
    }

    @Benchmark
    public void testSplittableRandom(BenchmarkState state) {
        for (int i = 0; i < ITERATIONS; i++) {
            state.splittableRandom.nextInt();
        }
    }
}
